package com.flyme.module.file.provider.controller;


import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.security.OpenHelper;
import com.flyme.common.core.security.OpenUserAuth;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.entity.SysUser;
import com.flyme.module.file.client.api.SysIndextagRemoteService;
import com.flyme.module.file.client.entity.SysIndextag;
import com.flyme.module.file.provider.service.SysCourseService;
import com.flyme.module.file.provider.service.SysIndextagService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author flyme
 */
@Slf4j
@Api(tags = "首页模块管理")
@RestController
public class SysIndextagController extends BaseController<SysIndextagService, SysIndextag> implements SysIndextagRemoteService {

    @Autowired
    private SysCourseService courseService;

    @Override
    public ResultBody list(PagerInfo pagerInfo, SysIndextag indextag) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("*");
        cq.ne("isShow", 0);
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    @Override
    public ResultBody addOrEdit(SysIndextag indextag){
        Boolean tag = bizService.saveOrUpdate(indextag);
        return ResultBody.success("操作成功").setData(tag);
    }

    @Override
    public ResultBody delByTagId(@PathVariable Long id) {
        Integer courseNum = courseService.countByTag(id);
        if(courseNum > 0){
            return ResultBody.failed("该模块下有课程，无法删除");
        }
        Boolean tag = bizService.removeById(id);
        return ResultBody.success("操作成功");
    }

}
